/**
 * Created by Vivienfanghua on 2016/2/24.
 */

var symbolSVG = {
    "植物": "Symbol_Tree",
    "飞机": "Symbol_Airplane",
    "房子": "Symbol_House"
}
var Global_Symbols_type={
    "几何符号":{
        "平面符号":{
            "圆形":"GSPFCL",
            "三角形":"GSPFTR",
            "矩形":"GSPFRE",
            "多边形":"GSPFPO",
            "星形":"GSPOST"
        },
        "立体符号":{
            "圆柱":"GSSFCL",
            "三棱柱":"GSSFTR",
            "四棱柱":"GSSFRE"
        }
    },
    "艺术符号":{
        "自然条件":{
            "地质":"ASNCGE",
            "地貌":"ASNCLA",
            "气候":"ASNCCL",
            "水文":"ASNCHY",
            "土壤":"ASNCSO",
            "植被":"ASNCVE",
            "动物":"ASNCAN",
            "自然保护区":"ASNCNP"
        },
        "自然资源":{
            "土地资源":"ASNRLR",
            "水资源":"ASNRWR",
            "矿产资源":"ASNRWR",
            "森林资源":"ASNRTR",
            "海洋资源":"ASNROR",
            "旅游资源":"ASNRTO"
        },
        "基础设施":{
            "交通运输":"ASBITP",
            "邮电通信":"ASBIPT"
        },
        "社会经济":{
            "人口":"ASCEPO",
            "综合经济":"ASCEEN",
            "文化教育":"ASCEED",
            "医疗卫生":"ASCEHC"
        },
        "生态环境": {
            "自然灾害": "ASECND",
            "生态环境变化":"ASECEE",
            "环境污染":"ASECEP"
        }
    },
    "文字符号":{
        "自然条件":{
            "地质":"ASNCGE",
            "地貌":"ASNCLA",
            "气候":"ASNCCL",
            "水文":"ASNCHY",
            "土壤":"ASNCSO",
            "植被":"ASNCVE",
            "动物":"ASNCAN",
            "自然保护区":"ASNCNP"
        },
        "自然资源":{
            "土地资源":"ASNRLR",
            "水资源":"ASNRWR",
            "矿产资源":"ASNRWR",
            "森林资源":"ASNRTR",
            "海洋资源":"ASNROR",
            "旅游资源":"ASNRTO"
        },
        "基础设施":{
            "交通运输":"ASBITP",
            "邮电通信":"ASBIPT"
        },
        "社会经济":{
            "人口":"ASCEPO",
            "综合经济":"ASCEEN",
            "文化教育":"ASCEED",
            "医疗卫生":"ASCEHC"
        },
        "生态环境": {
            "自然灾害": "ASECND",
            "生态环境变化":"ASECEE",
            "环境污染":"ASECEP"
        }
    }
}
var Global_Symbols = [
    'ASBIPT01',
    'ASBITP01',
    'ASCEED01',
    'ASCEED02',
    'ASCEEN01',
    'ASCEEN02',
    'ASCEEN03',
    'ASCEEN04',
    'ASCEEN05',
    'ASCEEN06',
    'ASCEEN07',
    'ASCEHC01',
    'ASCEPO01',
    'ASCEPO02',
    'ASECEE01',
    'ASECEP01',
    'ASECND01',
    'ASNCAN01',
    'ASNCAN02',
    'ASNCAN03',
    'ASNCCL01',
    'ASNCGE01',
    'ASNCHY01',
    'ASNCLA01',
    'ASNCNP01',
    'ASNCSO01',
    'ASNCVE01',
    'ASNCVE02',
    'ASNCVE03',
    'ASNCVE04',
    'ASNRLR01',
    'ASNRMR01',
    'ASNROR01',
    'ASNRTO01',
    'ASNRTR01',
    'ASNRWR01',
    'GSPFCI01',
    'GSPFCI02',
    'GSPFCI03',
    'GSPFPO01',
    'GSPFPO02',
    'GSPFPO03',
    'GSPFRE01',
    'GSPFRE02',
    'GSPFRE03',
    'GSPFST01',
    'GSPFST02',
    'GSPFST03',
    'GSPFTR01',
    'GSSFCI01',
    'GSSFCI02',
    'GSSFPO01',
    'GSSFRE01',
    'GSSFST01',
    'GSSFTR01'];

function pieChart(rootname, name, data, cx, cy, r, colors) {
    var svg = Snap(rootname);
    var g = svg.paper.g();
    g.attr({id: name});
    var total = 0;
    //data=data.slice(2);
    data.forEach(function (value) {
        total += value;
    });
    var angles = data.map(function (x) {
        return x / total * Math.PI * 2;
    });
    var startangle = 0;
    for (var i = 0; i < data.length; i++) {
        var endangle = startangle + angles[i];
        var x1 = cx + r * Math.sin(startangle);
        var y1 = cy - r * Math.cos(startangle);
        var x2 = cx + r * Math.sin(endangle);
        var y2 = cy - r * Math.cos(endangle);
        var big = 0;
        if (endangle - startangle > Math.PI)
            big = 1;
        var d = "M " + cx + "," + cy + " L " + x1 + "," + y1 + "," + " A " + r + "," + r + " 0 " + big + " 1 " + x2 + "," + y2 + " Z";
        var path = svg.path(d).attr({fill: colors[i], stroke: "black", strokeWidth: "2"});
        g.add(path);
        startangle = endangle;
    }
    return g;
}

function pieLegend(rootname, name, cx, cy, r, colors, labels, legendName, fontsize, datalength) {
    var svg = Snap(rootname);
    var g = svg.paper.g();
    g.attr({id: name});
    //var rect=svg.rect(cx-2.2*width,cy-2*height,5*r,4*r,10);
    //rect.attr({fill:"#f6f6f6",stroke:"black",strokeWidth:"2"});
    //g.add(rect);
    var total = 0;
    data = [];
    for (var i = 0; i < datalength; i++)
        data.push(1);
    data.forEach(function (value) {
        total += value;
    });
    var angles = data.map(function (x) {
        return x / total * Math.PI * 2;
    });
    var startangle = 0;
    for (var i = 0; i < data.length; i++) {
        var endangle = startangle + angles[i];
        var x1 = cx + r * Math.sin(startangle);
        var y1 = cy - r * Math.cos(startangle);
        var x2 = cx + r * Math.sin(endangle);
        var y2 = cy - r * Math.cos(endangle);
        var x3 = cx + r * Math.sin((startangle + endangle) / 2);
        var y3 = cx - r * Math.cos((startangle + endangle) / 2);
        if (x3 < cx)
            x3 = x3 - 1.5 * r;
        var big = 0;
        if (endangle - startangle > Math.PI)
            big = 1;
        var d = "M " + cx + "," + cy + " L " + x1 + "," + y1 + "," + " A " + r + "," + r + " 0 " + big + " 1 " + x2 + "," + y2 + " Z";
        var path = svg.path(d).attr({fill: colors[i], stroke: "black", strokeWidth: "2"});
        g.add(path);
        startangle = endangle;
        var colorrect = svg.rect(cx + 1.1 * r, cy - r + 1.5 * r * i / data.length, 0.2 * r, 0.2 * r, 0);
        var colorrectheight = colorrect.getBBox().height;
        colorrect.attr({fill: colors[i], stroke: "black", strokeWidth: "2"});
        var textone = svg.text(cx + 1.4 * r, cy - r + 1.5 * r * i / data.length + colorrectheight, labels[i]);
        //var text=svg.text(x3,y3,labels[i]);
        textone.attr({"font-weight": "normal", "font-size": fontsize + "px", "font-family": "Adobe 仿宋 Std R"});
        //text.drag();
        g.add(textone);
        g.add(colorrect);
    }
    var text2 = svg.text(cx, cy, legendName);
    text2.attr({"font-weight": "bold", "font-size": 1.5 * fontsize + "px", "font-family": "Adobe 仿宋 Std R"});
    var namewidth = text2.getBBox().width;
    var nameheight = text2.getBBox().height;
    text2.attr({x: cx - 0.5 * namewidth, y: cy - r - 0.5 * nameheight});
    g.add(text2);
    //svg.append(g);
    return g;
}

//id为要着色的对象，color为color.js中的颜色
function setcolor(id, color) {
    var colors = color.split(",");
    Snap("#" + id).attr({fill: "#" + parseInt(colors[0]).toString(16) + parseInt(colors[1]).toString(16) + parseInt(colors[2]).toString(16)});
}

function setcolor2(id, color) {
    var colors = color.split(",");
    var root = document.getElementById(id);
    var childrens = root.children;
    for (var i = 0; i < childrens.length; i++)
        childrens[i].setAttribute("fill", "#" + parseInt(colors[0]).toString(16) + parseInt(colors[1]).toString(16) + parseInt(colors[2]).toString(16));
}

//分级底色
//Input:数组元素,分组后的类型,色带
function ColorMap(Data, type, colorband) {

}

//条形图
function barChart(rootname, name, data, cx, cy, heightPer, widthPer, colors) {
    var svg = Snap(rootname);
    var g = svg.paper.g();
    g.attr({id: name});
    //data=data.slice(2);
    for (var i = 0; i < data.length; i++) {
        var xone = parseInt(cx + widthPer * i);
        var heightone = data[i] * heightPer;
        var yone = parseInt(cy - heightone);
        var rect = svg.rect(xone, yone, widthPer, heightone);
        rect.attr({fill: colors[i], stroke: "black", strokeWidth: "2"});
        g.add(rect);
    }
    return g;
}

//条形图图例
function bandLegend(rootname, name, labels, number, cx, cy, heightPer, widthPer, strokeWidth, maxValue, colors, fontsize) {
    var svg = Snap(rootname);
    var g = svg.paper.g();
    g.attr({id: name});
    var nearBigValue = Math.pow(10, Math.log10(maxValue).toFixed(0));
    var maxHeight = parseInt(heightPer * nearBigValue * 0.51);
    var maxWidth = parseInt(2 * number * widthPer + widthPer);
    var p1 = svg.polyline([cx, cy - maxHeight, cx, cy]).attr({
        stroke: "#000",
        strokeWidth: strokeWidth
    });
    g.add(p1);
    var p11 = svg.polyline([cx, cy, cx + maxWidth, cy]).attr({
        stroke: "#000",
        strokeWidth: strokeWidth
    });
    g.add(p11);
    for (var i = 1; i <= 5; i++) {
        var oney = parseInt(cy - nearBigValue * heightPer * 0.1 * i);
        var p2 = svg.polyline(cx, oney, cx + widthPer * 0.2, oney).attr({
            stroke: "#000",
            strokeWidth: parseInt(strokeWidth * 0.5)
        });
        g.add(p2);
        var textone = svg.text(cx, oney, "" + i).attr({
            "font-weight": "normal", "font-size": fontsize + "px", "font-family": "宋体"
        });
        var namewidth = textone.getBBox().width;
        textone.attr({x: parseInt(cx - namewidth * 1.1)});
        g.add(textone);
    }
    var svgunit = svg.text(cx, cy - maxHeight, "" + nearBigValue * 0.1).attr({
        "font-weight": "normal", "font-size": fontsize + "px", "font-family": "宋体"
    });
    g.add(svgunit);
    for (var i = 0; i < number; i++) {
        var xone = parseInt(cx + (2 * i + 1) * widthPer);
        var heightone = parseInt(Math.random() * maxHeight);
        var yone = cy - heightone;
        var rectone = svg.rect(xone, yone, widthPer, heightone).attr({
            fill: colors[i], stroke: "black", strokeWidth: "2"
        });
        g.add(rectone);
        var labelone = svg.text(xone, cy, labels[i]).attr({
            "font-weight": "normal", "font-size": fontsize + "px", "font-family": "宋体"
        });
        var labenoneheight = labelone.getBBox().height;
        labelone.attr({
            cy: cy + labenoneheight
        })
        labelone.transform(new Snap.Matrix().rotate(45, xone, cy + labenoneheight));
        g.add(labelone);
    }
    return g;
}

function Rating(Data, ranknum, type) {
    var result;
    var returnvalue = {};
    var maxvalue = Math.max.apply(null, Data);
    var minvalue = Math.min.apply(null, Data);
    returnvalue["min"] = minvalue;
    returnvalue["max"] = maxvalue;
    switch (type) {
        case "equalDiff":
            result = equalDiff(Data, ranknum);
            break;
        case "equalNum":
            result = equalNum(Data, ranknum);
            result = unique(result);
            break;
    }
    returnvalue["type"] = Data.map(function (d) {
        var one = -1;
        for (var i = 0; i < result.length - 1; i++)
            if (d >= result[i] && d < result[i + 1]) {
                one = i;
                break;
            }
        if (one == -1)
            one = result.length - 1;
        return one;
    });
    returnvalue["value"] = result;
    return returnvalue;
}

//去除重复元素，针对等数量分级可能造成数值相等的情况
function unique(array) {
    var result = [];
    array.forEach(function (d) {
        if (result.indexOf(d) == -1)
            result.push(d);
    });
    return result;
}

//等差分级
//包括下界不包括上界
function equalDiff(Data, ranknum) {
    var result = [];
    var maxvalue = Math.max.apply(null, Data);
    var minvalue = Math.min.apply(null, Data);
    var pervalue = (maxvalue - minvalue) / (ranknum - 1);
    for (var i = 0; i < ranknum; i++)
        result.push(minvalue + i * pervalue);
    return result;
}

//等数量分级
//包括下界不包括上界
function equalNum(Data, ranknum) {
    var result = [];
    var sortData = Data.slice(0).sort();
    var pernum = parseInt(Data.length / ranknum);
    for (var i = 0; i < ranknum; i++)
        result.push(sortData[pernum * i]);
    return result;
}

function Legend_C_Pie(rootname, name, width, height, cx, cy, minsize, maxsize, valuearray, fontsize) {
    var dataset = {startAngle: 0, endAngle: Math.PI};
    var sizearray = getDData(minsize, maxsize, valuearray, 4);
}

//连续比率
//Input:最小尺寸，最大尺寸，数组
//Output:尺寸数组
function getCData(minsize, maxsize, valuesarray) {
    var maxvalue = Math.max.apply(null, valuesarray);
    var minvalue = Math.min.apply(null, valuesarray);
    var valuerange = maxvalue - minvalue;
    var sizerange = maxsize - minsize;
    var returnvalue = [];
    return valuesarray.map(function (d) {
        return (d - minvalue) / valuerange * sizerange + minsize;
    })
}

//间隔比率
//Input:最小尺寸，最大尺寸，数组，间隔数
//Output:尺寸数组
function getDData(minsize, maxsize, valuesarray, internum) {
    var num = internum - 1;
    var maxvalue = Math.max.apply(null, valuesarray);
    var minvalue = Math.min.apply(null, valuesarray);
    var valuerange = maxvalue - minvalue;
    var pervalue = valuerange / num;
    var sizerange = maxsize - minsize;
    var persize = sizerange / num;
    var returnvalue = [];
    return valuesarray.map(function (d) {
        return parseInt(1.0 * (d - minvalue) / pervalue) * persize + minsize;
    })
}